from rest_framework.views import APIView
from rest_framework.response import Response
from .models import UserModel, InformationModel
import re

class RegisterAPIView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        if not all([username, password]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        user_data = UserModel.objects.filter(username=username)
        if user_data:
            return Response({
                'code': 400,
                'msg': '该用户已存在'
            })
        UserModel.objects.create(
            username=username,
            password=password
        )
        return Response({
            'code': 201,
            'msg': '注册成功'
        })

class LoginAPIView(APIView):
    def get(self, reqeust):
        username = reqeust.data.get('username')
        password = reqeust.data.get('password')
        if not all([username, password]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        user_data = UserModel.objects.filter(username=username)
        print(user_data)
        if not user_data:
            return Response({
                'code': 400,
                'msg': '该用户不存在'
            })
        print(type(user_data))
        if password!=user_data[0].password:
            return Response({
                'code': 400,
                'msg': '密码错误'
            })
        return Response({
            'code': 200,
            'msg': '登录成功'
        })

class AddMobileEmail(APIView):
    def post(self, request):
        user = request.data.get('username')
        mobile = request.data.get('phone')
        email = request.data.get('email')
        if not all([user, mobile, email]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        if not re.findall(r'^1[3-9]\d{9}$', mobile):
            return Response({
                'code': 400,
                'msg': '手机号格式错误'
            })
        user_data = UserModel.objects.get(username=user)
        try:
            user_data.email = email
            user_data.mobile = mobile
            user_data.save()
            return Response({
                'code': 200,
                'msg': '绑定成功'
            })
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '绑定失败'
            })

    def put(self, request):
        user = request.data.get('username')
        mobile = request.data.get('mobile')
        email = request.data.get('email')
        if not all([user, mobile, email]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        if not re.findall(r'^1[3-9]\d{9}$', mobile):
            return Response({
                'code': 400,
                'msg': '手机号格式错误'
            })
        user_data = UserModel.objects.get(username=user)
        try:
            user_data.email = email
            user_data.mobile = mobile
            user_data.save()
            return Response({
                'code': 200,
                'msg': '修改成功'
            })
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '修改失败'
            })

class MainAPIView(APIView):
    def get(self, request):
        user = request.data.get('username')
        uid = UserModel.objects.get(username=user).id
        # print('uid======================>>', uid)
        try:
            user_data = InformationModel.objects.filter(user_id=uid)
            print(user_data)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败'
            })
        user_list = [{
            'img': i.img,
            'briefly': i.briefly,
            'count': i.count,
            'time': i.time,
        } for i in user_data]
        return Response({
            'code': 200,
            'msg': '获取成功',
            'information': user_list
        })